package edu.tongji.jiaowu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import com.tongji.quickCRUDQ.model.CommonBean;
import com.tongji.quickCRUDQ.service.impl.BaseManagerImpl;
import com.tongji.quickCRUDQ.util.JsonDataSet;

import edu.tongji.jiaowu.service.BasicDataManager;
import edu.tongji.jiaowu.service.GradeMajorManager;
import edu.tongji.jiaowu.service.MajorClassManager;
import edu.tongji.jiaowu.service.MajorDirectionManager;
import edu.tongji.jiaowu.service.ParameterManager;
import edu.tongji.jiaowu.service.VcodeManager;
import edu.tongji.jiaowu.service.VyearCollegeManager;

public class BasicDataManagerImpl extends BaseManagerImpl implements BasicDataManager {
	private ParameterManager parameterManager;

	private VcodeManager vcodeManager;

	private VyearCollegeManager vyearCollegeManager;

	private GradeMajorManager gradeMajorManager;
	private MajorDirectionManager majorDirectionManager;
	private MajorClassManager majorClassManager;

/**
 * 查询所有 学年度、学期、年级、系、专业
 * 将查询结果json,返回客户端形成combo
 */
	public Map semesterGradeMajorMap() {
		Map map = semeterMap();
		Map model = gradeMajorMap();
		model.putAll(map);
		return model;
	}

/**
 * 查询所有 年级、系、专业
 * 将查询结果json,返回客户端形成combo
 */
	public Map gradeMajorMap() {
		Map model = new HashMap();
		CommonBean grades = vcodeManager.findCodes("GRADE");
		String gradeJson = JsonDataSet.commonbeanToJsonArray(grades, "code", "name");
		CommonBean yearColleges = vyearCollegeManager.findAllVyearColleges();
		String yearCollegeJson = JsonDataSet.commonbeanToJsonArray(yearColleges, new String[]{"collegeId", "collegeName", "schoolYear","collegeNo"});
		CommonBean gradeMajors = gradeMajorManager.findAllGradeMajors();
		String jsonArray = JsonDataSet.commonbeanToJsonArray(gradeMajors, new String[]{"majorNo","majorName","grade","refid","ID"});
		
		model.put("gradeJson", gradeJson);
		model.put("yearCollegeJson", yearCollegeJson);
		model.put("gradeMajorJson", jsonArray);
		return model;
	}

/**
 * 查询所有年级、系,具有专业方向的专业,所有专业方向
 * 将查询结果格式化成json,返回客户端形成combo
 */
	public Map gradeMajorWithDirectionMap() {
		Map model = new HashMap();
		CommonBean grades = vcodeManager.findCodes("GRADE");
		CommonBean yearColleges = vyearCollegeManager.findAllVyearColleges();
		CommonBean gradeMajors=gradeMajorManager.findDirectionMajors();
        adjustByGradeMajor(grades, yearColleges, gradeMajors);
        
		String gradeJson = JsonDataSet.commonbeanToJsonArray(grades, "code", "name");
		String yearCollegeJson = JsonDataSet.commonbeanToJsonArray(yearColleges, new String[]{"collegeId", "collegeName", "schoolYear","collegeNo"});
		String jsonArray = JsonDataSet.commonbeanToJsonArray(gradeMajors, new String[]{"majorNo","majorName","grade","refid","ID"});
		
		CommonBean directions=majorDirectionManager.findValidMajorDirections();
		String directionArray=JsonDataSet.commonbeanToJsonArray(directions, new String[]{"ID","directionName","directionNo","refid"});  
		
		model.put("gradeJson", gradeJson);
		model.put("yearCollegeJson", yearCollegeJson);
		model.put("gradeMajorJson", jsonArray);
		model.put("directionJson", directionArray);

		return model;
	}

	
	public Map gradeMajorWithClassMap() {
		Map model = new HashMap();
		CommonBean grades = vcodeManager.findCodes("GRADE");
		CommonBean yearColleges = vyearCollegeManager.findAllVyearColleges();
		CommonBean gradeMajors=gradeMajorManager.findSchoolClassMajors();
        adjustByGradeMajor(grades, yearColleges, gradeMajors);
        
		String gradeJson = JsonDataSet.commonbeanToJsonArray(grades, "code", "name");
		String yearCollegeJson = JsonDataSet.commonbeanToJsonArray(yearColleges, new String[]{"collegeId", "collegeName", "schoolYear","collegeNo"});
		String jsonArray = JsonDataSet.commonbeanToJsonArray(gradeMajors, new String[]{"majorNo","majorName","grade","refid","ID"});
		
		CommonBean schoolClasses=majorClassManager.findValidMajorClasss();
		String classArray=JsonDataSet.commonbeanToJsonArray(schoolClasses, new String[]{"ID","className","classNo","refid"});  
		
		model.put("gradeJson", gradeJson);
		model.put("yearCollegeJson", yearCollegeJson);
		model.put("gradeMajorJson", jsonArray);
		model.put("classJson", classArray);

		
		return model;
	}
	
	public Map gradeMajorDirectionMap() {
		Map model = new HashMap();
		CommonBean grades = vcodeManager.findCodes("GRADE");
		CommonBean yearColleges = vyearCollegeManager.findAllVyearColleges();
		CommonBean gradeMajors=gradeMajorManager.findAllGradeMajors();
        
		String gradeJson = JsonDataSet.commonbeanToJsonArray(grades, "code", "name");
		String yearCollegeJson = JsonDataSet.commonbeanToJsonArray(yearColleges, new String[]{"collegeId", "collegeName", "schoolYear","collegeNo"});
		String jsonArray = JsonDataSet.commonbeanToJsonArray(gradeMajors, new String[]{"majorNo","majorName","grade","refid","ID"});
		
		CommonBean directions=majorDirectionManager.findValidMajorDirections();
		String directionArray=JsonDataSet.commonbeanToJsonArray(directions, new String[]{"ID","directionName","directionNo","refid"});  
		
		model.put("gradeJson", gradeJson);
		model.put("yearCollegeJson", yearCollegeJson);
		model.put("gradeMajorJson", jsonArray);
		model.put("directionJson", directionArray.toString());

		return model;
	}
	
	

	/**
	 * @param year 毕业年份
	 * 查询在"year"年毕业的所有年级、系,专业
	 * 将查询结果json,返回客户端形成combo
	 */
	public Map graduateMajorMap(String year) {
		Map model = new HashMap();
		CommonBean grades = vcodeManager.findCodes("GRADE");
		CommonBean yearColleges = vyearCollegeManager.findAllVyearColleges();
		CommonBean gradeMajors = gradeMajorManager.findGraduateMajors(year);
        adjustByGradeMajor(grades, yearColleges, gradeMajors);
        
		String gradeJson = JsonDataSet.commonbeanToJsonArray(grades, "code", "name");
		String yearCollegeJson = JsonDataSet.commonbeanToJsonArray(yearColleges, new String[]{"collegeId", "collegeName", "schoolYear","collegeNo"});
		String jsonArray = JsonDataSet.commonbeanToJsonArray(gradeMajors, new String[]{"majorNo","majorName","grade","refid","ID"});
		
		model.put("gradeJson", gradeJson);
		model.put("yearCollegeJson", yearCollegeJson);
		model.put("gradeMajorJson", jsonArray);
		return model;
	}

/**
 * 
* @authorhfm
*功能：根据年级专业,调整年级、系的数据(只保留与年级专业gradeMajors有关联的数据)
* @param args
 */	
	private void adjustByGradeMajor(CommonBean grades, CommonBean yearColleges, CommonBean gradeMajors) {
		for (int i = 0; i < grades.getSize(); ) {
			String grade=grades.getPropertyAsString(i, "code");
			CommonBean cb1=gradeMajors.query("grade",new BigDecimal(grade));
			if (cb1.getSize()==0) {
				grades.remove("code", grade);
			}else{
				i++;
			}
		}
        for (int i = 0; i < yearColleges.getSize(); ) {
        	String schoolYear=yearColleges.getPropertyAsString(i, "schoolYear");
        	CommonBean cb1=grades.query("code",schoolYear);
        	if (cb1.getSize()==0) {
        		yearColleges.remove("schoolYear", new BigDecimal(schoolYear));
        		i=0;
        	}else{
        		i++;
        	}
        }
		
        for (int i = 0; i < yearColleges.getSize(); ) {
        	String collegeId=yearColleges.getPropertyAsString(i, "collegeId");
        	CommonBean cb1=gradeMajors.query("refid",collegeId);
        	if (cb1.getSize()==0) {
        		yearColleges.remove("collegeId", collegeId);
        		i=0;
        	}else{
        		i++;
        	}
        }
	}
/**
 * 
* @authorhfm
*功能：将directions格式化成json
* @param directions 专业方向
	private StringBuffer directionJson(CommonBean directions) {
		StringBuffer jsonArray = new StringBuffer("[");
		int s1 = directions.getSize();
		for (int i = 0; i < s1; i++) {
			String value = directions.getId(i);
			String text = directions.getPropertyAsString(i, "directionName");
			String directionNo = directions.getPropertyAsString(i, "directionNo");
			String majorId = directions.getPropertyAsString(i, "refid");
			jsonArray.append("['").append(value).append("','").append(text).append("','").append(directionNo).append("','").append(majorId).append("']");
			if (i < s1 - 1) {
				jsonArray.append(",");
			}
		}
		jsonArray.append("]");
		return jsonArray;
	}
 */
	
/*	private StringBuffer gradeMajorJson(CommonBean gradeMajors) {
		StringBuffer jsonArray = new StringBuffer("[");
		int s1 = gradeMajors.getSize();
		for (int i = 0; i < s1; i++) {
			String value = gradeMajors.getPropertyAsString(i, "majorNo");
			String text = gradeMajors.getPropertyAsString(i, "majorName");
			String grade = gradeMajors.getPropertyAsString(i, "grade");
			String refid = gradeMajors.getPropertyAsString(i, "refid");
			String id = gradeMajors.getId(i);
			jsonArray.append("['").append(value).append("','").append(text).append("','").append(grade).append("','").append(refid).append("','").append(id).append("']");
			if (i < s1 - 1) {
				jsonArray.append(",");
			}
		}
		jsonArray.append("]");
		return jsonArray;
	}*/
	
/**
 * 查询所有学年度学期,并格式化成json后存入map
 */
	public Map semeterMap() {
		Map model = new HashMap();
		CommonBean years = vcodeManager.findCodes("SCHOOL_YEAR");
		String yearJson = JsonDataSet.commonbeanToJsonArray(years, "code", "name");
		CommonBean semesters = vcodeManager.findCodes("SCHOOL_SEMESTER");
		String semesterJson = JsonDataSet.commonbeanToJsonArray(semesters, "code", "name");
		model.put("yearJson", yearJson);
		model.put("semesterJson", semesterJson);
		return model;
	}

	public GradeMajorManager getGradeMajorManager() {
		return gradeMajorManager;
	}

	public void setGradeMajorManager(GradeMajorManager gradeMajorManager) {
		this.gradeMajorManager = gradeMajorManager;
	}

	public ParameterManager getParameterManager() {
		return parameterManager;
	}

	public void setParameterManager(ParameterManager parameterManager) {
		this.parameterManager = parameterManager;
	}

	public VcodeManager getVcodeManager() {
		return vcodeManager;
	}

	public void setVcodeManager(VcodeManager vcodeManager) {
		this.vcodeManager = vcodeManager;
	}

	public VyearCollegeManager getVyearCollegeManager() {
		return vyearCollegeManager;
	}

	public void setVyearCollegeManager(VyearCollegeManager vyearCollegeManager) {
		this.vyearCollegeManager = vyearCollegeManager;
	}

	public MajorDirectionManager getMajorDirectionManager() {
		return majorDirectionManager;
	}

	public void setMajorDirectionManager(MajorDirectionManager majorDirectionManager) {
		this.majorDirectionManager = majorDirectionManager;
	}

	public MajorClassManager getMajorClassManager() {
		return majorClassManager;
	}

	public void setMajorClassManager(MajorClassManager majorClassManager) {
		this.majorClassManager = majorClassManager;
	}



}
